package logic

import (
	"context"
	"errors"
	"person/models/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePerfectLogic {
	return &UpdatePerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatePerfectLogic) UpdatePerfect(in *personrpc.UpdatePerfectRequest) (*personrpc.UpdatePerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	user.Age = in.Age
	user.Email = in.Email
	user.Address = in.Address

	if err := user.UpdatePerfect(mysql.DB, in.UserId); err != nil {
		return nil, err
	}

	return &personrpc.UpdatePerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
